package com.aoyy.action.product;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ModelDriven;
import com.aoyy.action.BaseAction;
import com.aoyy.model.ProductType;
import com.aoyy.util.Pagination;
/**=============================================================================
 * <code>ProductTypeAction</code> action for <code>ProductType</code>
 * @author Administrator
 * @date 2017-07-13
 * ============================================================================== */
@Scope("prototype")
@Controller("productTypeAction")
public class ProductTypeAction extends BaseAction implements ModelDriven<ProductType> {
  private static final long serialVersionUID = 5557476297132531728L;
  private Integer parentTypeId;
  private ProductType productType;
  private Pagination<ProductType> pagination;
  
  public void setParentTypeId(Integer parentTypeId) {
	this.parentTypeId = parentTypeId;  
  }
  public Integer getParentTypeId() {
	return parentTypeId;  
  }
  
  public void setProductType(ProductType productType) {
	this.productType = productType;  
  }
  public ProductType getProductType() {
    return productType;	  
  }
  
  public Pagination<ProductType> getPagination() {
	return pagination; 
  }
  
  @Override
  public ProductType getModel() {
    return productType;	  
  }
  
  public String add() {
	productType = new ProductType();
	if (parentTypeId != null && parentTypeId > 0) {
	  ProductType parent = productTypeDao.load(parentTypeId);
	  productType.setLevel(parent.getLevel() + 1);
	} else {
	  productType.setLevel(1);
	}
	return ADD;
  }
  
  public String save() {
	if (productType.getName() == null || productType.getName().isEmpty()) {
	  addFieldError("name", "Name is required.");
	  return ADD;
	}
	if (!productTypeDao.isUniqueProductType(productType, 0)) {
	  addFieldError("name", "The product type using this name exits!");
	  return ADD;
	}
	if (parentTypeId != null && parentTypeId > 0)  {
      productType.setParent(productTypeDao.load(parentTypeId));
    }
    productTypeDao.save(productType);
    return list();
  }
  
  public String list() {
	String where;
	Object[] parameters = null;
	if (parentTypeId != null && parentTypeId > 0) {
	  where = "where parent.typeId = ?";
	  parameters = new Integer[] {parentTypeId};
	} else {
	  where = "where parent.typeId is null";	
	}
	pagination = productTypeDao.query(where, parameters, pageNo, pageSize);
	return LIST;  
  }
  
  public String edit() {
	Integer typeId = productType.getTypeId();
	if (typeId != null && typeId >0) {
      productType = productTypeDao.get(typeId);
    }
	return EDIT;
  }
  
  public String update() {
	if (productType.getName() == null || productType.getName().isEmpty()) {
	  addFieldError("name", "Name is required.");
	  return EDIT;
	}
	if (!productTypeDao.isUniqueProductType(productType, 1)) {
	  addFieldError("name", "The product type using this name exits!");
	  return EDIT;
	}
	ProductType productType2 = productTypeDao.load(productType.getTypeId());
	productType2.setName(productType.getName());
	productTypeDao.saveOrUpdate(productType2);
	return list();
  }
  
  public String delete() {
	Integer typeId = productType.getTypeId();
	if (typeId != null && typeId > 0) {
	  productTypeDao.delete(typeId);  	
	}
	return list();
  }

}
